//
//  WMPublicMsgViewController.m
//  witMorning
//
//  Created by Mac mini on 2018/1/2.
//  Copyright © 2018年 zt. All rights reserved.
//

#import "WMPublicMsgViewController.h"
#import "WMComNewsTable.h"
#import "WMBasicNewsTable.h"
#import "NPHomeHeaderBar.h"

#define head_height 40

@interface WMPublicMsgViewController ()<homeHeaderBarDelegate,UIScrollViewDelegate>
@end

@implementation WMPublicMsgViewController
{
    NPHomeHeaderBar *headerBar;
    UIScrollView *mainScroll;
    WMComNewsTable *comTable;
    WMBasicNewsTable *basicTable;
    
    NSArray *comArray;

}



- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"信息公告";
    self.view.backgroundColor = [UIColor whiteColor];
    [self createBar];
    [self createMainScroll];
    [self requestData];
    
//    self.automaticallyAdjustsScrollViewInsets = NO;
   
}

-(void)createBar{
    headerBar= [[NPHomeHeaderBar alloc]initWithFrame:CGRectMake(0, NAV_HEIGHT, SCREEN_W, head_height) andTitleArray:@[@"公司新闻",@"基层动态"]];
    headerBar.barDelegate = self;
    [self.view addSubview:headerBar];
}

-(void)createMainScroll{
    NSArray<UITableView *> *viewArr = @[self.comTable,self.basicTable];
    
    mainScroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0,head_height+NAV_HEIGHT,SCREEN_W,CGRectGetHeight(self.view.frame)-head_height-NAV_HEIGHT)];
    mainScroll.delegate = self;
    [self.view addSubview:mainScroll];
    
    for (int i = 0; i<viewArr.count; i++) {
        UITableView *view = viewArr[i];
        [mainScroll addSubview:view];
    }
    mainScroll.pagingEnabled = YES;
    [mainScroll setContentSize:CGSizeMake(2*SCREEN_W, 0)];
    
}
                        
-(WMComNewsTable *)comTable{
    if (!comTable) {
        comTable = [[WMComNewsTable alloc]initWithFrame:CGRectMake(0, 0, SCREEN_W, SCREEN_H-head_height)  style:UITableViewStyleGrouped];
        comTable.controller = self;
        
        if (@available(iOS 11.0, *)) {
            comTable. contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }else{
            self.automaticallyAdjustsScrollViewInsets = NO;
        }
        
        
    }
    return comTable;
}

-(WMBasicNewsTable *)basicTable{
    if (!basicTable) {
        basicTable = [[WMBasicNewsTable alloc]initWithFrame:CGRectMake(SCREEN_W, 0, SCREEN_W, SCREEN_H-head_height) style:UITableViewStyleGrouped];
    }
    return basicTable;
}

-(void)requestData{
    
    NSString *url = [BASE_HTTP stringByAppendingString:@"getNewInfo"];
    [[WMManage shareInstance]getRequestWithUrl:url success:^(id responseObj) {
        
        comArray = [NSArray arrayWithArray:responseObj];
        
        comTable.dataArray = comArray;
        
    } fail:^(id error) {
        NSLog(@"失败:%@",error);
    }];
    //
}


#pragma mark ----homeHeaderBarDelegate--点击----
-(void)homeHeaderBarClickWithIndex:(NSInteger)index{
    [mainScroll setContentOffset:CGPointMake(SCREEN_W*index, 0)];

}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat offset = mainScroll.contentOffset.x/(SCREEN_W*2);
    [headerBar setScrollLabWithX:offset];
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    NSInteger page = mainScroll.contentOffset.x/SCREEN_W;
    headerBar.selectButton.selected = NO;
    headerBar.buttonArray[page].selected = YES;
    headerBar.selectButton = headerBar.buttonArray[page];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
